import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { createPinia } from 'pinia'


import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import '@/assets/less/index.less'
import * as ElementPlusIconsVue from "@element-plus/icons-vue";

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

import "@/api/mock.js";//引入mockjs
import api from "@/api/api";

function isRoute(to) {
  let res= router.getRoutes()
  let resFil = res.filter((item) => item.path === to.path);
  return resFil.length > 0;
  // return router.getRoutes().filter((item) => item.path === to.path).length > 0;
}

router.beforeEach((to, from,next) => {
  NProgress.start();
  if (to.path !== "/login" && !store.state.token) {
    return { name: "login" };
    
  }
  if (!isRoute(to)) {
    // alert('123')
    router.push('/login');//正常跳转到404页面
  }
  next()
});
router.afterEach((to, from) => {
  NProgress.done();
})
import { useAllDataStore } from "@/stores";
const pinia = createPinia();
const app = createApp(App)
  const store = useAllDataStore(pinia);
  app.config.globalProperties.$api = api; //挂载到全局
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
  }
app.use(ElementPlus)
app.use(pinia);
store.addMenu(router,"refresh");
app.use(router).mount('#app')
